package com.cci.kangdao.controller;

import com.cci.kangdao.service.UserService;
import com.cci.kangdao.utilTool.ParameterTool;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author William Du
 * @date 2019/10/14
 */
@Controller
public class UserController {

    private Logger log = Logger.getLogger(this.getClass().getName());

    @Resource
    private UserService userService;

    /**
     * 获取用户信息
     *
     * @param request  request
     * @param response response
     * @return null
     */
    @RequestMapping("/getUserInfo.aspx")
    public String getUserInfo(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            userService.getUserInfo(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    /**
     * 更新用户指引类型
     *
     * @param request  request
     * @param response response
     * @return null
     */
    @RequestMapping("/updateGuideType.aspx")
    public String updateGuideType(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            userService.updateGuideType(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    /**
     * 更新首次登录弹框的字段
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/updateIsShowComplete.aspx")
    public String updateIsShowComplete(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            userService.updateIsShowComplete(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    /**
     * App用户修改密码接口
     * @param request
     * @return
     */
    @RequestMapping("/updateUserInfo.aspx")
    public String updateUserInfo(HttpServletRequest request,
                                       HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            userService.updateUserInfo(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    /**
     * 小蜜蜂打开用户修改标签接口
     * @param userLabels
     * @return
     */
    @RequestMapping(value = "/upholdUserLabel.aspx", method = RequestMethod.POST)
    public String upholdUserLabel(String userLabels,
                                 HttpServletResponse response){
        try{
            boolean success = userService.upholdUserLabel(userLabels);
            if(success){
                ParameterTool.writeSuccessResponse(response,success);
            } else {
                ParameterTool.writeResponse(response,-45,"保存失败",success);
            }
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    // Guidanz功能权限-Guidanz功能权限管理
    @RequestMapping("/checkGMAuth.aspx")
    public String checkGMAuth(HttpServletRequest request,HttpServletResponse response) {
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            userService.checkUserAcce(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    @RequestMapping("/getAuthUser.aspx")
    public String getAuthUser(HttpServletRequest request,HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            userService.getUserByToken(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    @RequestMapping("/getUser.aspx")
    public String getUser(HttpServletRequest request,HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            userService.getUserById(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    //移动技师获取用户推送短信相关信息
    @RequestMapping("/getUserInfoByMT.aspx")
    public ModelAndView getUserInfoByMT(HttpServletRequest request,
                                               HttpServletResponse response) throws Exception {
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return userService.getUserInfoByMT(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }

    /**
     * Guidanz需求
     * Guidanz申请权限-》获取用户信息
     */
    @RequestMapping("/getUserInfoGuidanz.aspx")
    @ResponseBody
    public void getUserInfoGuidanz(HttpServletRequest request, HttpServletResponse response) {
    	  try{
              Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
              map.put("response", response);
              userService.getUserInfoGuidanz(map);
          }catch(Exception ex){
              log.error(ex.getMessage(),ex);
              ParameterTool.writeErrorResponse(response);
          }
    }

    /**
     * 根据手机号获取用户
     */
    @RequestMapping("/getUserInfoByPhone.aspx")
    @ResponseBody
    public void getUserInfoByPhone(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            userService.getUserInfoByPhone(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
    }

    /**
     * 根据用户获取用户标签
     */
    @RequestMapping("/getUserLabel.aspx")
    @ResponseBody
    public void getUserLabel(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            userService.getUserLabel(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
    }

    /**
     * 首次登录会变成  会变成红点
     */
    @RequestMapping("/updateUserLabel.aspx")
    @ResponseBody
    public void updateUserLabel(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            userService.updateOrUpdateSerLabel(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
    }

}
